<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 7.3.0">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32.ico">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16.ico">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css" integrity="sha256-wiz7ZSCn/btzhjKDQBms9Hx4sSeUYsDrTLg7roPstac=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.28/fancybox/fancybox.css" integrity="sha256-6cQIC71/iBIYXFK+0RHAvwmjwWzkWd+r7v/BX3/vZDc=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/themes/green/pace-theme-minimal.css">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/pace.min.js" integrity="sha256-gqd7YTjg/BtfqWSwsJOvndl0Bxc8gFImLEkXQT8+qj0=" crossorigin="anonymous"></script>

<script class="next-config" data-name="main" type="application/json">{"hostname":"sumumm.github.io","root":"/","images":"/images","scheme":"Gemini","darkmode":false,"version":"8.19.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":true,"style":"mac"},"fold":{"enable":true,"height":300},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":true,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>

    <meta name="description" content="本文主要是Git中git rebase的相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。">
<meta property="og:type" content="article">
<meta property="og:title" content="LV01-Git-04-Git本地仓库-09-rebase">
<meta property="og:url" content="https://sumumm.github.io/post/6805396b.html">
<meta property="og:site_name" content="苏木">
<meta property="og:description" content="本文主要是Git中git rebase的相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-09-rebase/img/image-20230623173710631.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-09-rebase/img/image-20230623175457017.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-09-rebase/img/image-20230623175805573.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-09-rebase/img/image-20230623180830638.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-09-rebase/img/image-20230623181113883.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-09-rebase/img/image-20230623181825490.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-09-rebase/img/image-20230623182109610.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-09-rebase/img/image-20230623182233510.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-09-rebase/img/image-20230623182419147.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-09-rebase/img/image-20230623182927118.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-09-rebase/img/image-20230623184406589.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-09-rebase/img/image-20230623185241501.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-09-rebase/img/image-20230623185451988.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-09-rebase/img/image-20230623185954097.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-09-rebase/img/image-20230623190618678.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-09-rebase/img/image-20230623191149027.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-09-rebase/img/image-20230623191614200.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-09-rebase/img/image-20230623214231762.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-09-rebase/img/image-20230623214435924.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-09-rebase/img/image-20230623214644527.png">
<meta property="article:published_time" content="2023-07-02T07:14:29.000Z">
<meta property="article:modified_time" content="2025-06-13T16:25:57.070Z">
<meta property="article:author" content="苏木">
<meta property="article:tag" content="Git">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-09-rebase/img/image-20230623173710631.png">


<link rel="canonical" href="https://sumumm.github.io/post/6805396b.html">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"https://sumumm.github.io/post/6805396b.html","path":"post/6805396b.html","title":"LV01-Git-04-Git本地仓库-09-rebase"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>LV01-Git-04-Git本地仓库-09-rebase | 苏木</title>
  








    <script src="/js/browser_tools_disable.js"></script>

  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
<!-- hexo injector head_end start --><link rel="stylesheet" href="https://unpkg.com/hexo-next-tags-plus@latest/lib/tag_plus.css" media="defer" onload="this.media='all'"><!-- hexo injector head_end end --></head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">苏木</p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">我的学习之路</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>苏木的家</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类页<span class="badge">42</span></a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档页<span class="badge">673</span></a></li><li class="menu-item menu-item-flink"><a href="/flink/" rel="section"><i class="fa fa-link fa-fw"></i>友人帐</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于我</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%80%E3%80%81%E5%88%86%E5%8F%89%E7%9A%84%E6%8F%90%E4%BA%A4%E5%8E%86%E5%8F%B2"><span class="nav-text">一、分叉的提交历史</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E6%8F%90%E4%BA%A4%E5%8E%86%E5%8F%B2%E5%88%86%E5%8F%89%E7%9A%84%E6%83%85%E5%86%B5"><span class="nav-text">1. 提交历史分叉的情况</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E4%BB%93%E5%BA%93%E5%87%86%E5%A4%87"><span class="nav-text">2. 仓库准备</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-1-%E5%A6%82%E4%BD%95%E4%BA%A7%E7%94%9F%E5%88%86%E5%8F%89"><span class="nav-text">2.1 如何产生分叉</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-2-%E5%88%86%E5%8F%89%E7%9A%84%E5%88%86%E6%94%AF%E5%87%86%E5%A4%87"><span class="nav-text">2.2 分叉的分支准备</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#2-2-1-%E5%88%B0C2%E6%97%B6master%E5%88%86%E6%94%AF%E7%9A%84main-c"><span class="nav-text">2.2.1 到C2时master分支的main.c</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-2-4-%E6%96%B0%E5%BB%BAdev%E5%88%86%E6%94%AF%E5%B9%B6%E6%8F%90%E4%BA%A4"><span class="nav-text">2.2.4 新建dev分支并提交</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-2-3-%E5%88%87%E6%8D%A2%E5%88%B0master%E5%88%86%E6%94%AF%E5%B9%B6%E6%8F%90%E4%BA%A4"><span class="nav-text">2.2.3 切换到master分支并提交</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E5%88%86%E5%8F%89%E4%BA%A7%E7%94%9F"><span class="nav-text">3. 分叉产生</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-master%E5%88%86%E6%94%AF%E5%9B%9E%E5%88%B0%E6%9C%AA%E5%90%88%E5%B9%B6%E7%9A%84%E6%97%B6%E5%80%99"><span class="nav-text">4. master分支回到未合并的时候</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BA%8C%E3%80%81%E5%8F%98%E5%9F%BA%E5%9F%BA%E6%9C%AC%E6%93%8D%E4%BD%9C"><span class="nav-text">二、变基基本操作</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E5%8F%98%E5%9F%BA%E6%93%8D%E4%BD%9C"><span class="nav-text">1. 变基操作</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-1-%E5%88%87%E6%8D%A2%E5%88%B0dev%E5%88%86%E6%94%AF"><span class="nav-text">1.1 切换到dev分支</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-2-%E5%8F%98%E5%9F%BA%E5%88%B0master"><span class="nav-text">1.2 变基到master</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-3-%E8%A7%A3%E5%86%B3%E5%86%B2%E7%AA%81%E5%B9%B6%E6%8F%90%E4%BA%A4"><span class="nav-text">1.3 解决冲突并提交</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-4-%E7%BB%A7%E7%BB%AD%E5%8F%98%E5%9F%BA"><span class="nav-text">1.4 继续变基</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-5-dev%E5%88%86%E6%94%AF%E5%B7%AE%E5%BC%82%E5%88%86%E6%9E%90"><span class="nav-text">1.5 dev分支差异分析</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-6-%E5%BF%AB%E8%BF%9B%E5%90%88%E5%B9%B6"><span class="nav-text">1.6 快进合并</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-7-%E6%9F%A5%E7%9C%8Bmaster%E6%8F%90%E4%BA%A4%E8%AE%B0%E5%BD%95"><span class="nav-text">1.7 查看master提交记录</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E5%8E%9F%E7%90%86%E5%88%86%E6%9E%90"><span class="nav-text">2. 原理分析</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-1-%E5%8F%98%E5%9F%BA%E8%BF%87%E7%A8%8B%E4%B8%AD"><span class="nav-text">2.1 变基过程中</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-2-%E5%8F%98%E5%9F%BA%E5%AE%8C%E6%88%90"><span class="nav-text">2.2 变基完成</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-3-%E5%BF%AB%E8%BF%9B%E5%90%88%E5%B9%B6"><span class="nav-text">2.3 快进合并</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%89%E3%80%81%E5%90%88%E5%B9%B6%E6%8F%90%E4%BA%A4%E8%AE%B0%E5%BD%95"><span class="nav-text">三、合并提交记录</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E6%83%85%E5%86%B5%E8%AF%B4%E6%98%8E"><span class="nav-text">1. 情况说明</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E5%91%BD%E4%BB%A4%E8%AF%B4%E6%98%8E"><span class="nav-text">2. 命令说明</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E7%BC%96%E8%BE%91%E5%99%A8%E4%B8%AD%E7%9A%84%E5%91%BD%E4%BB%A4"><span class="nav-text">3. 编辑器中的命令</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">4. 使用实例</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#4-1-%E6%9F%A5%E7%9C%8B%E6%8F%90%E4%BA%A4%E8%AE%B0%E5%BD%95"><span class="nav-text">4.1 查看提交记录</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-2-%E5%90%88%E5%B9%B6%E6%9C%80%E6%96%B03%E6%9D%A1%E6%8F%90%E4%BA%A4%E8%AE%B0%E5%BD%95"><span class="nav-text">4.2 合并最新3条提交记录</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-3-%E4%BF%AE%E6%94%B9%E5%90%88%E5%B9%B6%E5%90%8E%E7%9A%84%E6%8F%90%E4%BA%A4%E8%AF%B4%E6%98%8E"><span class="nav-text">4.3 修改合并后的提交说明</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-4-%E6%9F%A5%E7%9C%8B%E6%8F%90%E4%BA%A4%E8%AE%B0%E5%BD%95"><span class="nav-text">4.4 查看提交记录</span></a></li></ol></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="苏木"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">苏木</p>
  <div class="site-description" itemprop="description">莫道桑榆晚，为霞尚满天</div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">673</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">42</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
        <span class="site-state-item-count">43</span>
        <span class="site-state-item-name">标签</span>
      </div>
  </nav>
</div>
  <div class="links-of-author animated">
      <span class="links-of-author-item">
        <a href="https://github.com/sumumm" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;sumumm" rel="noopener me" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
  </div>

        </div>
      </div>
    </div>

    
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://sumumm.github.io/post/6805396b.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="苏木">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="苏木">
      <meta itemprop="description" content="莫道桑榆晚，为霞尚满天">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="LV01-Git-04-Git本地仓库-09-rebase | 苏木">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          LV01-Git-04-Git本地仓库-09-rebase
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2023-07-02 15:14:29" itemprop="dateCreated datePublished" datetime="2023-07-02T15:14:29+08:00">2023-07-02</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/" itemprop="url" rel="index"><span itemprop="name">开发工具</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/" itemprop="url" rel="index"><span itemprop="name">03版本控制</span></a>
        </span>
    </span>

  
    <span class="post-meta-break"></span>
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>6k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>22 分钟</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody"><p>本文主要是Git中<code>git rebase</code>的相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。</p>
<span id="more"></span>

<!-- Photo: https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-09-rebase/img/ -->

<details class="folding-tag" blue><summary> 点击查看使用工具及版本 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center" width=150px>Windows</td>        <td align="left">windows11</td>    </tr>    <tr>        <td align="center">Ubuntu</td>        <td align="left">Ubuntu16.04的64位版本</td>      </tr>    <tr>        <td align="center">VMware® Workstation 16 Pro</td>        <td align="left">16.2.3 build-19376536</td>      </tr></table>
              </div>
            </details>

<details class="folding-tag" blue><summary> 点击查看本文参考资料 </summary>
              <div class='content'>
              <table>    <tr><td align="center">参考方向  </td><td align="center">参考原文</td></tr>    <tr><td align="left">Git 官网</td><td align="left"><a href="https://git-scm.com/" target="_blank">https://git-scm.com/ <i class="fa fa-external-link-alt"></i> </a></td></tr>    <tr><td align="left">Git 官方文档</td><td align="left"><a href="https://git-scm.com/doc" target="_blank">https://git-scm.com/doc <i class="fa fa-external-link-alt"></i> </a></td></tr>    <tr><td align="left">Pro Git Book</td><td align="left"><a href="https://git-scm.com/book/zh/v2" target="_blank">https://git-scm.com/book/zh/v2 <i class="fa fa-external-link-alt"></i> </a></td></tr>    <tr><td align="left">Git 快速使用指南</td><td align="left"><a href="https://training.github.com/downloads/zh_CN/github-git-cheat-sheet/" target="_blank">https://training.github.com/downloads/zh_CN/github-git-cheat-sheet/ <i class="fa fa-external-link-alt"></i> </a></td></tr>    <tr><td align="left">Visual Git Cheat Sheet</td><td align="left"><a href="https://ndpsoftware.com/git-cheatsheet.html#loc=index" target="_blank">https://ndpsoftware.com/git-cheatsheet.html#loc=index <i class="fa fa-external-link-alt"></i> </a></td></tr></table>
              </div>
            </details>

<details class="folding-tag" blue><summary> 点击查看相关文件下载 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center">---</td>        <td align="left">--- <a href="" target="_blank">  <i class="fa fa-external-link-alt"></i></a></td>      </tr></table>
              </div>
            </details>

<p>【说明】本节笔记的相关操作在Windows下进行，因为VS Code有个Git的插件，可以很直观的演示一些东西。由于Git安装后自带一个Git-Bash终端，所以就不用Win下的命令行啦，就用的这个终端，因为它里边的命令与Linux很类似，而windows中的命令行有些命令与linux并不相同，为了统一，还是用用Git自带的终端啦。另外VS Code是可以选择使用的终端的，我直接将VS Code使用的终端改成了git-bash，这样更方便一些。</p>
<p>在 Git 中整合来自不同分支的修改主要有两种方法：<code>merge</code> 以及 <code>rebase</code>。 我们将学习什么是“变基”，怎样使用“变基”，并将展示该操作的惊艳之处，以及指出在何种情况下应避免使用它。</p>
<h1 id="一、分叉的提交历史"><a href="#一、分叉的提交历史" class="headerlink" title="一、分叉的提交历史"></a><font size=3>一、分叉的提交历史</font></h1><h2 id="1-提交历史分叉的情况"><a href="#1-提交历史分叉的情况" class="headerlink" title="1. 提交历史分叉的情况"></a><font size=3>1. 提交历史分叉的情况</font></h2><p>前边学习分支的合并的时候，我们知道这样的仓库情况，在进行合并之后，会产生分叉：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-09-rebase/img/image-20230623173710631.png" alt="image-20230623173710631" style="zoom:50%;" />

<h2 id="2-仓库准备"><a href="#2-仓库准备" class="headerlink" title="2. 仓库准备"></a><font size=3>2. 仓库准备</font></h2><h3 id="2-1-如何产生分叉"><a href="#2-1-如何产生分叉" class="headerlink" title="2.1 如何产生分叉"></a><font size=3>2.1 如何产生分叉</font></h3><p>上边的图是怎么产生的呢？需要经过以下步骤：</p>
<p>（1）master分支进行C0、C1和C2三次提交；</p>
<p>（2）创建dev分支，并切换到dev分支，此时dev分支从C2开始，然后在此分支进行修改，然后提交一次C4；</p>
<p>（3）切换到master，在C2之后，提交一次C3；</p>
<p>经过上边的这几个步骤之后，我们通过<code>git merge</code>合并的话，就会产生分叉。</p>
<h3 id="2-2-分叉的分支准备"><a href="#2-2-分叉的分支准备" class="headerlink" title="2.2 分叉的分支准备"></a><font size=3>2.2 分叉的分支准备</font></h3><h4 id="2-2-1-到C2时master分支的main-c"><a href="#2-2-1-到C2时master分支的main-c" class="headerlink" title="2.2.1 到C2时master分支的main.c"></a><font size=3>2.2.1 到C2时master分支的main.c</font></h4><details class="folding-tag" blue><summary> 点击查看main.c文件 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;unistd.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">// C0功能实现</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">func_C0</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;C0功能开始执行!\n&quot;</span>);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;C0&gt;&gt;&gt;&gt;&gt;&gt;步骤!\n&quot;</span>);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;C0功能执行完毕!\n&quot;</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// C1功能实现</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">func_C1</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;C1功能开始执行!\n&quot;</span>);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;C1&gt;&gt;&gt;&gt;&gt;&gt;步骤!\n&quot;</span>);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;C1功能执行完毕!\n&quot;</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// C2功能实现</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">func_C2</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;C2功能开始执行!\n&quot;</span>);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;C2&gt;&gt;&gt;&gt;&gt;&gt;步骤!\n&quot;</span>);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;C2功能执行完毕!\n&quot;</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">const</span> <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="comment">/* code */</span></span><br><span class="line">    <span class="keyword">while</span> (<span class="number">1</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        func_C0(); <span class="comment">// 实现C0功能</span></span><br><span class="line">        func_C1(); <span class="comment">// 实现C1功能</span></span><br><span class="line">        func_C2(); <span class="comment">// 实现C2功能</span></span><br><span class="line">        sleep(<span class="number">1</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<details class="folding-tag" blue><summary> 点击查看提交记录 </summary>
              <div class='content'>
              <figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">git mylog</span></span><br><span class="line">* [qidaink] 7e54c61 : feat:C2(实现C2功能并提交)  (HEAD -&gt; master) (2023-06-23 17:44:59)</span><br><span class="line">* [qidaink] da60a44 : feat:C1(实现C1功能并提交)  (2023-06-23 17:44:34)</span><br><span class="line">* [qidaink] 3614b67 : feat:C0(实现C0功能并提交)  (2023-06-23 17:43:58)</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h4 id="2-2-4-新建dev分支并提交"><a href="#2-2-4-新建dev分支并提交" class="headerlink" title="2.2.4 新建dev分支并提交"></a><font size=3>2.2.4 新建dev分支并提交</font></h4><ul>
<li>新建并切换到dev分支</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git checkout -b dev</span><br></pre></td></tr></table></figure>

<ul>
<li>修改文件内容</li>
</ul>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-09-rebase/img/image-20230623175457017.png" alt="image-20230623175457017" style="zoom:50%;" />

<ul>
<li>提交C4修改</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git commit -a -m &quot;feat:C4(dev分支修改C0 C1 C2功能并提交)&quot;</span><br></pre></td></tr></table></figure>

<ul>
<li>查看提交记录</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">git mylog</span> </span><br><span class="line">* [qidaink] 7aebfbd : feat:C4(dev分支修改C0 C1 C2功能并提交)  (HEAD -&gt; dev) (2023-06-23 17:55:58) </span><br><span class="line">* [qidaink] 7e54c61 : feat:C2(实现C2功能并提交)  (master) (2023-06-23 17:44:59) </span><br><span class="line">* [qidaink] da60a44 : feat:C1(实现C1功能并提交)  (2023-06-23 17:44:34) </span><br><span class="line">* [qidaink] 3614b67 : feat:C0(实现C0功能并提交)  (2023-06-23 17:43:58)</span><br></pre></td></tr></table></figure>



<h4 id="2-2-3-切换到master分支并提交"><a href="#2-2-3-切换到master分支并提交" class="headerlink" title="2.2.3 切换到master分支并提交"></a><font size=3>2.2.3 切换到master分支并提交</font></h4><ul>
<li>切换到master分支</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git checkout master</span><br></pre></td></tr></table></figure>

<ul>
<li>修改文件内容</li>
</ul>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-09-rebase/img/image-20230623175805573.png" alt="image-20230623175805573" style="zoom:50%;" />

<ul>
<li>提交C3修改</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git commit -a -m &quot;feat:C3(master分支修改C0 C1 C2功能并提交)&quot;</span><br></pre></td></tr></table></figure>

<ul>
<li>查看提交记录</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">git mylog</span> </span><br><span class="line">* [qidaink] 918dcbf : feat:C3(master分支修改C0 C1 C2功能并提交)  (HEAD -&gt; master) (2023-06-23 17:58:46) </span><br><span class="line">* [qidaink] 7e54c61 : feat:C2(实现C2功能并提交)  (2023-06-23 17:44:59)</span><br><span class="line">* [qidaink] da60a44 : feat:C1(实现C1功能并提交)  (2023-06-23 17:44:34)</span><br><span class="line">* [qidaink] 3614b67 : feat:C0(实现C0功能并提交)  (2023-06-23 17:43:58)</span><br></pre></td></tr></table></figure>

<h2 id="3-分叉产生"><a href="#3-分叉产生" class="headerlink" title="3. 分叉产生"></a><font size=3>3. 分叉产生</font></h2><p>我们此时切换到master分支，然后将dev分支的内容合并到master分支们就会产生分叉：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">git checkout master</span></span><br><span class="line">Already on &#x27;master&#x27;</span><br><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">git merge dev</span></span><br><span class="line">Auto-merging main.c</span><br><span class="line">CONFLICT (content): Merge conflict in main.c</span><br><span class="line">Automatic merge failed; fix conflicts and then commit the result.</span><br></pre></td></tr></table></figure>

<p>这种情况下一定会产生冲突，这里的重点不在解决冲突，我们迅速处理一下，直接提交：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git commit -a -m &quot;feat:C5(dev分支合并到master分支,解决冲突后提交)&quot;</span><br></pre></td></tr></table></figure>

<p>这个时候我们看一下提交记录的ASCII图：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git log --graph --oneline --decorate</span><br></pre></td></tr></table></figure>

<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-09-rebase/img/image-20230623180830638.png" alt="image-20230623180830638" style="zoom:67%;" />

<p>这个时候果然产生了分叉，整个版本库当前的情况如下：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-09-rebase/img/image-20230623181113883.png" alt="image-20230623181113883" style="zoom:50%;" />

<h2 id="4-master分支回到未合并的时候"><a href="#4-master分支回到未合并的时候" class="headerlink" title="4. master分支回到未合并的时候"></a><font size=3>4. master分支回到未合并的时候</font></h2><p>我们后边要学习变基，所以这里需要恢复到未合并dev分支的时候：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git reset --hard 918dcbf</span><br></pre></td></tr></table></figure>

<p>然后再查看master分支提交记录如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">git mylog</span> </span><br><span class="line">* [qidaink] 918dcbf : feat:C3(master分支修改C0 C1 C2功能并提交)  (HEAD -&gt; master) (2023-06-23 17:58:46) </span><br><span class="line">* [qidaink] 7e54c61 : feat:C2(实现C2功能并提交)  (2023-06-23 17:44:59) </span><br><span class="line">* [qidaink] da60a44 : feat:C1(实现C1功能并提交)  (2023-06-23 17:44:34) </span><br><span class="line">* [qidaink] 3614b67 : feat:C0(实现C0功能并提交)  (2023-06-23 17:43:58) </span><br></pre></td></tr></table></figure>

<h1 id="二、变基基本操作"><a href="#二、变基基本操作" class="headerlink" title="二、变基基本操作"></a><font size=3>二、变基基本操作</font></h1><p>我们还可以提取dev分支在 <code>C4</code> 中引入的补丁和修改，然后在master分支的 <code>C3</code> 的基础上应用一次，也就是说将 dev 分支 C4 中所作的修改直接添加到 master 分支的 C3 中。 在 Git 中，这种操作就叫做 <strong>变基（rebase）</strong>。 我们可以使用 <code>rebase</code> 命令将提交到某一分支上的所有修改都移至另一分支上，就好像“重新播放”一样。</p>
<h2 id="1-变基操作"><a href="#1-变基操作" class="headerlink" title="1. 变基操作"></a><font size=3>1. 变基操作</font></h2><h3 id="1-1-切换到dev分支"><a href="#1-1-切换到dev分支" class="headerlink" title="1.1 切换到dev分支"></a><font size=3>1.1 切换到dev分支</font></h3><p>我们首先切换到dev分支：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">git checkout dev</span></span><br><span class="line">Switched to branch &#x27;dev&#x27;</span><br></pre></td></tr></table></figure>

<p>此时仓库情况如下：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-09-rebase/img/image-20230623181825490.png" alt="image-20230623181825490" style="zoom:50%;" />

<h3 id="1-2-变基到master"><a href="#1-2-变基到master" class="headerlink" title="1.2 变基到master"></a><font size=3>1.2 变基到master</font></h3><p>我们执行以下命令，进行变基：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git rebase master</span><br></pre></td></tr></table></figure>

<p>然后会得到以下输出信息：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-09-rebase/img/image-20230623182109610.png" alt="image-20230623182109610" style="zoom:67%;" />

<p>看起来还是在报错，我们看一下当前所有文件的状态以及所处的分支：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">git branch</span><br><span class="line">git status</span><br></pre></td></tr></table></figure>

<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-09-rebase/img/image-20230623182233510.png" alt="image-20230623182233510" style="zoom: 67%;" />

<p>可以看到当前是处于一个自动产生的分支并不属于master，也不属于dev，并且文件是发生了变化的，我们通过VS Code查看一下文件发生了哪些变化：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-09-rebase/img/image-20230623182419147.png" alt="image-20230623182419147" style="zoom:50%;" />

<p>可以看到其实就跟我们使用<code>git merge</code>的时候一样，产生了冲突。</p>
<h3 id="1-3-解决冲突并提交"><a href="#1-3-解决冲突并提交" class="headerlink" title="1.3 解决冲突并提交"></a><font size=3>1.3 解决冲突并提交</font></h3><p>我们处理一下冲突，然后提交一次：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git commit -a -m &quot;feat:C4&#x27;(dev分支变基到master，解决冲突后提交)&quot;</span><br></pre></td></tr></table></figure>

<p>然后我们查看一下当前所处分支还有提交记录：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-09-rebase/img/image-20230623182927118.png" alt="image-20230623182927118" style="zoom:67%;" />

<p>发现还处于那个中间分支，并没有回到我们的master分支或者dev分支。</p>
<h3 id="1-4-继续变基"><a href="#1-4-继续变基" class="headerlink" title="1.4 继续变基"></a><font size=3>1.4 继续变基</font></h3><p>还记得前边的报错吧，报错提示信息如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">git rebase master</span></span><br><span class="line">Auto-merging main.c</span><br><span class="line">CONFLICT (content): Merge conflict in main.c</span><br><span class="line">error: could not apply 7aebfbd... feat:C4(dev分支修改C0 C1 C2功能并提交)</span><br><span class="line">hint: Resolve all conflicts manually, mark them as resolved with</span><br><span class="line">hint: &quot;git add/rm &lt;conflicted_files&gt;&quot;, then run &quot;git rebase --continue&quot;.</span><br><span class="line">hint: You can instead skip this commit: run &quot;git rebase --skip&quot;.</span><br><span class="line">hint: To abort and get back to the state before &quot;git rebase&quot;, run &quot;git rebase --abort&quot;.</span><br><span class="line">Could not apply 7aebfbd... feat:C4(dev分支修改C0 C1 C2功能并提交)</span><br></pre></td></tr></table></figure>

<p>这里其实已经提示我们了，我们有三个选择：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">git rebase --continue # 继续变基</span><br><span class="line">git rebase --abort    # 回到 git rebase 之前的状态</span><br><span class="line">git rebase --skip     # 跳过这个提交</span><br></pre></td></tr></table></figure>

<p>我们这里选择继续<code>git rebase --continue</code>，然后会看到有如下信息：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">git rebase --<span class="built_in">continue</span></span> </span><br><span class="line">Successfully rebased and updated refs/heads/dev.</span><br></pre></td></tr></table></figure>

<p>此时我们看一下当前所处的分支及提交记录：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-09-rebase/img/image-20230623184406589.png" alt="image-20230623184406589" style="zoom:67%;" />

<p>可以看到，中间分支消失了，我们回到了dev分支，然后之前dev分支的C4提交记录消失了，取而代之的是另外两条提交记录（只写发生变化的记录）：</p>
<figure class="highlight txt"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"># 原来的dev提交记录</span><br><span class="line">* [qidaink] 7aebfbd : feat:C4(dev分支修改C0 C1 C2功能并提交)  (HEAD -&gt; dev) (2023-06-23 17:55:58) </span><br><span class="line">* [qidaink] 7e54c61 : feat:C2(实现C2功能并提交)  (master) (2023-06-23 17:44:59) </span><br><span class="line">// 剩下的几条省略......</span><br><span class="line"></span><br><span class="line"># 变基之后的dev提交记录</span><br><span class="line">* [qidaink] 463111a : feat:C4&#x27;(在dev分支变基到master，解决冲突后提交)  (HEAD -&gt; dev) (2023-06-23 18:42:43) </span><br><span class="line">* [qidaink] 918dcbf : feat:C3(master分支修改C0 C1 C2功能并提交)  (master) (2023-06-23 17:58:46) </span><br><span class="line">* [qidaink] 7e54c61 : feat:C2(实现C2功能并提交)  (master) (2023-06-23 17:44:59) </span><br><span class="line">// 剩下的几条省略......</span><br><span class="line"></span><br><span class="line"># master分支的提交记录</span><br><span class="line">* [qidaink] 918dcbf : feat:C3(master分支修改C0 C1 C2功能并提交)  (HEAD -&gt; master) (2023-06-23 17:58:46) </span><br><span class="line">* [qidaink] 7e54c61 : feat:C2(实现C2功能并提交)  (2023-06-23 17:44:59)</span><br><span class="line">// 剩下的几条省略......</span><br></pre></td></tr></table></figure>

<h3 id="1-5-dev分支差异分析"><a href="#1-5-dev分支差异分析" class="headerlink" title="1.5 dev分支差异分析"></a><font size=3>1.5 dev分支差异分析</font></h3><p>我们来分析一下dev分支改变的那两次提交都改变了什么。</p>
<ul>
<li>（1）dev分支中C2和C3提交</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git difftool 7e54c61 918dcbf -y</span><br></pre></td></tr></table></figure>

<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-09-rebase/img/image-20230623185241501.png" alt="image-20230623185241501" style="zoom:50%;" />

<p>分析一下，这不就是我们master在C2之后的那次提交嘛。</p>
<ul>
<li>（2）dev分支中C3与C4‘提交</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git difftool 918dcbf 463111a -y</span><br></pre></td></tr></table></figure>

<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-09-rebase/img/image-20230623185451988.png" alt="image-20230623185451988" style="zoom:50%;" />

<p>这不就是之前我们在dev分支C2之后提交的C4的差异嘛。</p>
<h3 id="1-6-快进合并"><a href="#1-6-快进合并" class="headerlink" title="1.6 快进合并"></a><font size=3>1.6 快进合并</font></h3><p>经过上边的更改，发现了吗，dev分支似乎就是从master分支的C3提交之后新建的分支一样，这样不就相当于我们master分支开发到了C3，然后新建了分支dev，并在dev分支进行了新的提交，这个时候，我们直接merge，不就可以实现dev的修改合并到master了吗？我们执行以下命令：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">git checkout master</span></span><br><span class="line">Switched to branch &#x27;master&#x27;</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">git merge dev</span> </span><br><span class="line">Updating 918dcbf..463111a</span><br><span class="line">Fast-forward</span><br><span class="line"> main.c | 3 +++</span><br><span class="line"> 1 file changed, 3 insertions(+)</span><br></pre></td></tr></table></figure>

<h3 id="1-7-查看master提交记录"><a href="#1-7-查看master提交记录" class="headerlink" title="1.7 查看master提交记录"></a><font size=3>1.7 查看master提交记录</font></h3><p>我们来查看一下master分支的提交记录：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git log --graph --oneline --decorate</span><br></pre></td></tr></table></figure>

<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-09-rebase/img/image-20230623185954097.png" alt="image-20230623185954097" style="zoom:67%;" />

<p>我们发现此时这个分支没有分叉了，似乎变得很简洁，一条线很清楚。</p>
<h2 id="2-原理分析"><a href="#2-原理分析" class="headerlink" title="2. 原理分析"></a><font size=3>2. 原理分析</font></h2><h3 id="2-1-变基过程中"><a href="#2-1-变基过程中" class="headerlink" title="2.1 变基过程中"></a><font size=3>2.1 变基过程中</font></h3><p><code>git rebase</code>的原理是首先找到这两个分支（即当前分支 <code>dev</code>、变基操作的目标基底分支 <code>master</code>） 的最近共同祖先 <code>C2</code>，然后对比当前分支相对于该祖先的历次提交，<strong>提取相应的修改并存为临时文件</strong>， 然后将当前分支指向目标基底 <code>C3</code>, 最后以此将之前另存为临时文件的修改依序应用。 </p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-09-rebase/img/image-20230623190618678.png" alt="image-20230623190618678" style="zoom:50%;" />

<p>经过前边的过程，我们知道在变基过程中，也就是敲完<code>git rebase master</code>之后，产生了一个中间分支，叫<code>(no branch, rebasing dev)</code>，在次临时分支中，整合了dev分支中的C4的修改和master分支的C3的修改，由于修改的同一行，所以产生了冲突，我们在此临时分支上解决冲突，并提交了C4’。</p>
<p>这一步由于在整合两个分支的不同之处，所以，看起来更像是在C3的基础上加上了C4的修改。</p>
<h3 id="2-2-变基完成"><a href="#2-2-变基完成" class="headerlink" title="2.2 变基完成"></a><font size=3>2.2 变基完成</font></h3><p>当我们敲下<code>git rebase continue</code>的时候，完成了变基，此时，dev分支就变成了刚才的临时分支，包含了C4’提交：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-09-rebase/img/image-20230623191149027.png" alt="image-20230623191149027" style="zoom:50%;" />

<h3 id="2-3-快进合并"><a href="#2-3-快进合并" class="headerlink" title="2.3 快进合并"></a><font size=3>2.3 快进合并</font></h3><p>变基完成后，dev分支就像是从master分支的C3处新建分支，然后进行了C4’提交，这样的话，我们在master分支直接快进合并一次，就可以迅速将mater更新到C4’了：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-09-rebase/img/image-20230623191614200.png" alt="image-20230623191614200" style="zoom:50%;" />

<p>此时，<code>C4&#39;</code> 指向的快照就和前边分叉的时候的 <code>C5</code> 指向的快照一模一样了。 这两种整合方法的最终结果没有任何区别，但是变基使得提交历史更加整洁。 </p>
<p>我们在查看一个经过变基的分支的历史记录时会发现，尽管实际的开发工作是并行的， 但它们看上去就像是串行的一样，提交历史是一条直线没有分叉。一般我们这样做的目的是为了确保在向远程分支推送时能保持提交历史的整洁——例如向某个其他人维护的项目贡献代码时。 在这种情况下，我们首先在自己的分支里进行开发，当开发完成时你需要先将我们的代码变基到 <code>origin/master</code> 上，然后再向主项目提交修改。 这样的话，该项目的维护者就不再需要进行整合工作，只需要快进合并便可。</p>
<p>请注意，无论是通过变基，还是通过三方合并，整合的最终结果所指向的快照始终是一样的，只不过提交历史不同罢了。 变基是将一系列提交按照原有次序依次应用到另一分支上，而合并是把最终结果合在一起。</p>
<h1 id="三、合并提交记录"><a href="#三、合并提交记录" class="headerlink" title="三、合并提交记录"></a><font size=3>三、合并提交记录</font></h1><p><code>git rebase</code>命令不仅可以用与变基，使提交记录不产生分叉，还可以合并提交记录。</p>
<h2 id="1-情况说明"><a href="#1-情况说明" class="headerlink" title="1. 情况说明"></a><font size=3>1. 情况说明</font></h2><p><code>rebase</code> 可以让提交历史变得简洁，试想一下，一个功能的开发需要将近一个月的时间才能完成，在这一个月中不可能一次都不提交，这样提交的多了，提交记录就会显得特别的冗杂，这可不是大家希望看到的，这时候就会想，要是提交记录能合并该多好，于是 Git 中的 rebease 就为这种想法提供了可能。</p>
<h2 id="2-命令说明"><a href="#2-命令说明" class="headerlink" title="2. 命令说明"></a><font size=3>2. 命令说明</font></h2><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">合并从当前开始的前n个记录（包括当前）</span></span><br><span class="line">git rebase -i HEAD~n  # n 为要合并的提交记录数</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">合并指定版本号（不包含此版本）</span></span><br><span class="line">git rebase -i [commit_id]</span><br></pre></td></tr></table></figure>

<ul>
<li><code>-i（--interactive）</code>：弹出交互式的界面进行编辑合并</li>
<li><code>[commit_id]</code>：要合并多个版本之前的版本号，注意：<code>[commit_id]</code> 本身不参与合并，就相当于合并<code>(commit_id, HEAD]</code>这个区间内的提交记录。</li>
</ul>
<h2 id="3-编辑器中的命令"><a href="#3-编辑器中的命令" class="headerlink" title="3. 编辑器中的命令"></a><font size=3>3. 编辑器中的命令</font></h2><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">命令:</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">p, pick &lt;提交&gt; == 使用提交</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">r, reword &lt;提交&gt; == 使用提交，但编辑提交说明</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">e, edit &lt;提交&gt; == 使用提交，但停止以便在 shell 中修补提交</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">s, squash &lt;提交&gt; == 使用提交，但挤压到前一个提交</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">f, fixup [-C | -c] &lt;提交&gt; == 类似于 <span class="string">&quot;squash&quot;</span>，但只保留前一个提交</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">                           的提交说明，除非使用了 -C 参数，此情况下则只</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">                           保留本提交说明。使用 -c 和 -C 类似，但会打开</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">                           编辑器修改提交说明</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">x, <span class="built_in">exec</span> &lt;命令&gt; == 使用 shell 运行命令（此行剩余部分）</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">b, <span class="built_in">break</span> = 在此处停止（使用 <span class="string">&#x27;git rebase --continue&#x27;</span> 继续变基）</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">d, drop &lt;提交&gt; = 删除提交</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">l, label &lt;label&gt; = 为当前 HEAD 打上标记</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">t, reset &lt;label&gt; = 重置 HEAD 到该标记</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">m, merge [-C &lt;commit&gt; | -c &lt;commit&gt;] &lt;label&gt; [<span class="comment"># &lt;oneline&gt;]</span></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">.       创建一个合并提交，并使用原始的合并提交说明（如果没有指定</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">.       原始提交，使用注释部分的 oneline 作为提交说明）。使用</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">.       -c &lt;提交&gt; 可以编辑提交说明。</span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash"></span></span><br><span class="line"><span class="language-bash"><span class="comment"># 可以对这些行重新排序，将从上至下执行。</span></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash"></span></span><br><span class="line"><span class="language-bash"><span class="comment"># 如果您在这里删除一行，对应的提交将会丢失。</span></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash"></span></span><br><span class="line"><span class="language-bash"><span class="comment"># 然而，如果您删除全部内容，变基操作将会终止。</span></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash"></span></span><br><span class="line"><span class="language-bash"></span></span><br></pre></td></tr></table></figure>

<p><strong>【注意】</strong></p>
<p>（1）至少要有三次提交时才可以进行提交记录合并，也就是说，其他的提交记录不能与第一条提交记录合并，否则会报：<code>fatal: 无效的上游 ‘HEAD~2’</code>。</p>
<p>（2）在进入编辑器后，第一行的 pick 不要修改，其他的 pick 要改为 s ，这样提交记录才能进行合并，若是全部改为 s 则会报以下问题，出现该问题时，可以先终止，再重新合并。</p>
<h2 id="4-使用实例"><a href="#4-使用实例" class="headerlink" title="4. 使用实例"></a><font size=3>4. 使用实例</font></h2><h3 id="4-1-查看提交记录"><a href="#4-1-查看提交记录" class="headerlink" title="4.1 查看提交记录"></a><font size=3>4.1 查看提交记录</font></h3><p>我们用前边变基的仓库就好啦，我们来看一下master的提交记录：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">git mylog</span> </span><br><span class="line">* [qidaink] 463111a : feat:C4&#x27;(在dev分支变基到master，解决冲突后提交)  (HEAD -&gt; master, dev) (2023-06-23 18:42:43) </span><br><span class="line">* [qidaink] 918dcbf : feat:C3(master分支修改C0 C1 C2功能并提交)  (2023-06-23 17:58:46) </span><br><span class="line">* [qidaink] 7e54c61 : feat:C2(实现C2功能并提交)  (2023-06-23 17:44:59) </span><br><span class="line">* [qidaink] da60a44 : feat:C1(实现C1功能并提交)  (2023-06-23 17:44:34) </span><br><span class="line">* [qidaink] 3614b67 : feat:C0(实现C0功能并提交)  (2023-06-23 17:43:58) </span><br></pre></td></tr></table></figure>

<h3 id="4-2-合并最新3条提交记录"><a href="#4-2-合并最新3条提交记录" class="headerlink" title="4.2 合并最新3条提交记录"></a><font size=3>4.2 合并最新3条提交记录</font></h3><p>我们现在将C2、C3和C4’这三条提交记录合并成一条，可以输入以下命令：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git rebase -i HEAD~3</span><br></pre></td></tr></table></figure>

<p>然后变回打开git默认的编辑器，交互式的让我们进行操作：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-09-rebase/img/image-20230623214231762.png" alt="image-20230623214231762" style="zoom: 67%;" />

<p>我们保留第一个pick，将后边两行的pick改为s，改为s就表示将本次提交合并到前一个提交，然后保存，退出编辑器。</p>
<h3 id="4-3-修改合并后的提交说明"><a href="#4-3-修改合并后的提交说明" class="headerlink" title="4.3 修改合并后的提交说明"></a><font size=3>4.3 修改合并后的提交说明</font></h3><p>上一步完成后，会再次进入编辑器编辑模式，这一次是修改合并后需要显示的提交说明，这里没有#的部分将会作为我们合并后的提交说明，我们可以不修改直接提交，也可以注释掉重新写一个。</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-09-rebase/img/image-20230623214435924.png" alt="image-20230623214435924" style="zoom: 67%;" />

<p>我们将上边的三条记录修改成如下所示：</p>
<figure class="highlight txt"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">feat:C2~C4&#x27;合并后的提交</span><br></pre></td></tr></table></figure>

<p>然后保存退出编辑器即可。</p>
<h3 id="4-4-查看提交记录"><a href="#4-4-查看提交记录" class="headerlink" title="4.4 查看提交记录"></a><font size=3>4.4 查看提交记录</font></h3><p>我们输入查看提交记录的命令，可以看到提交记录已经被合并成1条了。</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-04-Git%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93-09-rebase/img/image-20230623214644527.png" alt="image-20230623214644527" style="zoom: 67%;" />

    </div>

    
    
    

    <footer class="post-footer">




    <div>
        
            <div style="text-align:center;color: #ccc;font-size:14px;">
            ----------本文结束
            <i class="fas fa-fan fa-spin" style="color: #FF1493; font-size: 1rem"></i>
            感谢您的阅读----------
            </div>
        
    </div>





  
  <div class="my_post_copyright"> 
    <p><span>文章标题:</span><a href="/post/6805396b.html">LV01-Git-04-Git本地仓库-09-rebase</a></p>
    <p><span>文章作者:</span><a href="/" title="欢迎访问 《苏木》 的学习笔记">苏木</a></p>
    <p><span>发布时间:</span>2023年07月02日 - 15:14</p>
    <p><span>最后更新:</span>2025年06月14日 - 00:25</p>
    <p><span>原始链接:</span><a href="/post/6805396b.html" title="LV01-Git-04-Git本地仓库-09-rebase">https://sumumm.github.io/post/6805396b.html</a></p>
    <p><span>许可协议:</span><i class="fab fa-creative-commons"></i> <a rel="license" href= "https://creativecommons.org/licenses/by-nc-nd/4.0/" target="_blank" title="Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0)">署名-非商业性使用-禁止演绎 4.0 国际</a> 转载请保留原文链接及作者。</p>  
  </div>
  


          <div class="post-tags">
              <a href="/tags/Git/" rel="tag"><i class="fa fa-tag"></i> Git</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/post/5abe0a5e.html" rel="prev" title="LV01-Git-04-Git本地仓库-10-贮藏与清理">
                  <i class="fa fa-angle-left"></i> LV01-Git-04-Git本地仓库-10-贮藏与清理
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/post/bc162ef0.html" rel="next" title="LV01-Git-04-Git本地仓库-08-查看差异">
                  LV01-Git-04-Git本地仓库-08-查看差异 <i class="fa fa-angle-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">

  <div class="copyright">
    &copy; 2017 – 
    <span itemprop="copyrightYear">2025</span>
    <span class="with-love">
      <i class="fa fa-heart"></i>
    </span>
    <span class="author" itemprop="copyrightHolder">苏木</span>
  </div>
<div class="wordcount">
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-line"></i>
    </span>
      <span>站点总字数：</span>
    <span title="站点总字数">3.7m</span>
  </span>
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span>站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">225:26</span>
  </span>
</div>




    <span id="sitetime"></span>
    <script defer language=javascript>
        function siteTime()
        {
            window.setTimeout("siteTime()", 1000);
            var seconds = 1000;
            var minutes = seconds * 60;
            var hours = minutes * 60;
            var days = hours * 24;
            var years = days * 365;
            var today = new Date();
            var todayYear = today.getFullYear();
            var todayMonth = today.getMonth()+1;
            var todayDate = today.getDate();
            var todayHour = today.getHours();
            var todayMinute = today.getMinutes();
            var todaySecond = today.getSeconds();
            /*==================================================
            Date.UTC() -- 返回date对象距世界标准时间(UTC)1970年1月1日午夜之间的毫秒数(时间戳)
            year        - 作为date对象的年份，为4位年份值
            month       - 0-11之间的整数，做为date对象的月份
            day         - 1-31之间的整数，做为date对象的天数
            hours       - 0(午夜24点)-23之间的整数，做为date对象的小时数
            minutes     - 0-59之间的整数，做为date对象的分钟数
            seconds     - 0-59之间的整数，做为date对象的秒数
            microseconds - 0-999之间的整数，做为date对象的毫秒数
            ==================================================*/
            var t1 = Date.UTC(2017, 
                              5, 
                              19, 
                              0, 
                              0, 
                              0); //北京时间
            var t2 = Date.UTC(todayYear,todayMonth,todayDate,todayHour,todayMinute,todaySecond);
            var diff = t2-t1;
            var diffYears = Math.floor(diff/years);
            var diffDays = Math.floor((diff/days)-diffYears*365);
            var diffHours = Math.floor((diff-(diffYears*365+diffDays)*days)/hours);
            var diffMinutes = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours)/minutes);
            var diffSeconds = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours-diffMinutes*minutes)/seconds);
            document.getElementById("sitetime").innerHTML="已在这里 "+diffYears+" 年 "+diffDays+" 天 "+diffHours+" 小时 "+diffMinutes+" 分钟 "+diffSeconds+" 秒";
        }
        siteTime();
    </script>



    </div>
  </footer>

  
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/next-theme-pjax/0.6.0/pjax.min.js" integrity="sha256-vxLn1tSKWD4dqbMRyv940UYw4sXgMtYcK6reefzZrao=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.28/fancybox/fancybox.umd.js" integrity="sha256-ytMJGN3toR+a84u7g7NuHm91VIR06Q41kMWDr2pq7Zo=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/lozad.js/1.16.0/lozad.min.js" integrity="sha256-mOFREFhqmHeQbXpK2lp4nA3qooVgACfh88fpJftLBbc=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script><script src="/js/pjax.js"></script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.1/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>




  <script src="/js/third-party/fancybox.js"></script>

  <script src="/js/third-party/pace.js"></script>


  




  

  <script class="next-config" data-name="enableMath" type="application/json">false</script><script class="next-config" data-name="mathjax" type="application/json">{"enable":true,"tags":"none","js":{"url":"https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-mml-chtml.js","integrity":"sha256-MASABpB4tYktI2Oitl4t+78w/lyA+D7b/s9GEP0JOGI="}}</script>
<script src="/js/third-party/math/mathjax.js"></script>


 
        <div id="click-show-text"
            data-mobile = false
            data-text = 富强,民主,文明,和谐,自由,平等,公正,法制,爱国,敬业,诚信,友善
            data-fontsize = 15px
            data-random= false>
        </div>
       

      
        <script async src=https://cdn.jsdelivr.net/npm/hexo-next-mouse-effect@latest/click/showText.js></script>
      

      
    




    <script async src="/js/fancybox_param.js"></script>





<!-- APlayer本体 -->



</body>
</html>
